package com.woniuxy.ptapp.app.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.util.BeanCopyUtil;
import com.woniuxy.commons.util.DateUtil;
import com.woniuxy.commons.util.GetTokenUserLogin;
import com.woniuxy.commons.util.Result;
import com.woniuxy.ptapp.client.ClassesCoachClient;
import com.woniuxy.ptapp.client.param.AddClassScheduleParam;
import com.woniuxy.ptapp.common.exception.EnumExceptionCode;
import com.woniuxy.ptapp.common.exception.ParamException;
import com.woniuxy.ptapp.common.exception.WoniuxyException;
import com.woniuxy.ptapp.common.param.LoginUser;
import com.woniuxy.ptapp.common.util.GetLogingUser;
import com.woniuxy.ptapp.infra.model.ClassSchedule;
import com.woniuxy.ptapp.infra.model.ClassesCoach;
import com.woniuxy.ptapp.infra.model.ClassesInfo;
import com.woniuxy.ptapp.service.ClassScheduleService;
import com.woniuxy.ptapp.service.ClassesCoachService;
import com.woniuxy.ptapp.service.ClassesInfoService;
import com.woniuxy.relation.client.AnnouncementClient;
import com.woniuxy.relation.client.form.SendAnnouncementForm;
import com.woniuxy.signing.client.CocahSigningVenueClient;
import com.woniuxy.signing.client.form.MakeSureSignForm;
import com.woniuxy.userapp.client.CoachClient;
import com.woniuxy.userapp.client.VenueClient;
import com.woniuxy.userapp.client.param.VenueInfo;
import io.swagger.annotations.Api;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangdong
 * @since 2022-07-30
 */
@RestController
public class ClassesCoachController implements ClassesCoachClient {
    @Resource
    private ClassesCoachService classesCoachService;
    @Resource
    private ClassesInfoService classesInfoService;
    @Resource
    private ClassScheduleService classScheduleService;
    @Resource
    private CocahSigningVenueClient cocahSigningVenueClient;
    @Resource
    private CoachClient coachClient;
    @Resource
    private GetLogingUser getLogingUser;
    @Resource
    private VenueClient venueClient;

    @Override
    public Result addClassesCoach(Integer coachId, Integer classesId) {
        if(null==coachId||null==classesId||coachId<=0||classesId<=0){
            throw new ParamException();
        }
        Boolean aBoolean = classesInfoService.checkClaessId(classesId);
        if(!aBoolean){
            throw new WoniuxyException(EnumExceptionCode.getClaessIdFail.getCode(),EnumExceptionCode.getClaessIdFail.getMessage());
        }
        //需要一个前置判断教练id是否存在
        ClassesCoach classesCoach = new ClassesCoach();
        classesCoach.setClassesId(classesId);
        classesCoach.setCoachId(coachId);
        boolean save = classesCoachService.save(classesCoach);
        if(!save){
            throw new WoniuxyException(EnumExceptionCode.addClassesCoachFail.getCode(),EnumExceptionCode.addClassesCoachFail.getMessage());
        }
        return Result.getInstance().setMsg("新增教练课程成功");
    }

    @Override
    public Result addClassSchedule(AddClassScheduleParam addClassScheduleParam) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse1 = sdf.parse(addClassScheduleParam.getClassesStartTime());
            Date parse2 = sdf.parse(addClassScheduleParam.getClassesEndTime());
        } catch (ParseException e) {
            throw new ParamException();
        }
        //获取登录用户
        LoginUser logingUser = getLogingUser.getLogingUser();
        //判断课程开始时间必须大于当前时间
        String nowtime = DateUtil.timeFormatString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        if(addClassScheduleParam.getClassesStartTime().compareTo(nowtime)<=0){
            throw new WoniuxyException(EnumExceptionCode.ClassTimeMustBegreaterThanTheCurrentTime.getCode(),EnumExceptionCode.ClassTimeMustBegreaterThanTheCurrentTime.getMessage());
        }
        //判断结束时间必须大于开始时间
        if(addClassScheduleParam.getClassesEndTime().compareTo(addClassScheduleParam.getClassesStartTime())<=0){
            throw new ParamException();
        }

        QueryWrapper<ClassesInfo> classesInfoQueryWrapper = new QueryWrapper<>();
        classesInfoQueryWrapper.eq("id",addClassScheduleParam.getClassesId());
        //判断课程id是否存在
        ClassesInfo one = classesInfoService.getOne(classesInfoQueryWrapper);
        if(one==null){
           throw new WoniuxyException(EnumExceptionCode.getClaessIdFail.getCode(),EnumExceptionCode.getClaessIdFail.getMessage() );
        }
        //判断教练id是否签约场馆id
        Result<VenueInfo> venueByAccountId = venueClient.findVenueByAccountId(logingUser.getLoginId());
        if(venueByAccountId==null||venueByAccountId.getCode()!=200){
            throw new WoniuxyException(EnumExceptionCode.ThereIsNoVenue.getCode(),EnumExceptionCode.ThereIsNoVenue.getMessage() );
        }
        MakeSureSignForm makeSureSignForm = new MakeSureSignForm();
        makeSureSignForm.setVenueId(venueByAccountId.getData().getId()+"");
        makeSureSignForm.setCocahId(addClassScheduleParam.getCoachId()+"");
        Result result = cocahSigningVenueClient.makeSureSign(makeSureSignForm);
        if (result == null || result.getCode() != 200) {
            throw new WoniuxyException(EnumExceptionCode.CoachVenueNoContractRelationship.getCode(), EnumExceptionCode.CoachVenueNoContractRelationship.getMessage());
        }
        //判断教练id是否存在
        Result<Integer> accountIdByCoachId = coachClient.findAccountIdByCoachId(addClassScheduleParam.getCoachId());
        if(accountIdByCoachId==null||accountIdByCoachId.getCode()!=200){
            throw new WoniuxyException(EnumExceptionCode.ThereIsNoCoach.getCode(),EnumExceptionCode.ThereIsNoCoach.getMessage());
        }
        //判断教练课程时间是否重复
        QueryWrapper<ClassSchedule> classScheduleQueryWrapper = new QueryWrapper<>();
        classScheduleQueryWrapper.eq("coach_id",addClassScheduleParam.getCoachId());
        List<ClassSchedule> classSchedules = classScheduleService.list(classScheduleQueryWrapper);
        if(classSchedules!=null||classSchedules.size()>0){
            List<String> collect = classSchedules.stream().map(m -> m.getClassesStartTime()).collect(Collectors.toList());
            if(collect.contains(addClassScheduleParam.getClassesStartTime())){
                throw new WoniuxyException(EnumExceptionCode.CourseOfTimeConflict.getCode(),EnumExceptionCode.CourseOfTimeConflict.getMessage());
            }
            //判断新增时间是否是在已有课程时间之内
            for (ClassSchedule classSchedule : classSchedules) {
                if(addClassScheduleParam.getClassesStartTime().compareTo(classSchedule.getClassesStartTime())>=0&&
                        addClassScheduleParam.getClassesStartTime().compareTo(classSchedule.getClassesEndTime())<=0
                ){
                   throw new WoniuxyException(EnumExceptionCode.CourseOfTimeConflict.getCode(),EnumExceptionCode.CourseOfTimeConflict.getMessage() );
                }
                if(addClassScheduleParam.getClassesEndTime().compareTo(classSchedule.getClassesStartTime())>=0&&
                        addClassScheduleParam.getClassesEndTime().compareTo(classSchedule.getClassesEndTime())<=0){
                    throw new WoniuxyException(EnumExceptionCode.CourseOfTimeConflict.getCode(),EnumExceptionCode.CourseOfTimeConflict.getMessage() );
                }
            }
        }
        ClassSchedule classSchedule = BeanCopyUtil.copyObject(addClassScheduleParam, ClassSchedule.class);
        classSchedule.setClassesName(one.getClassesName());
        boolean save = classScheduleService.save(classSchedule);
        if(!save){
           throw new WoniuxyException(EnumExceptionCode.TheNewCurriculumFailure.getCode(),EnumExceptionCode.TheNewCurriculumFailure
                   .getMessage());
        }

        return Result.getInstance().setMsg("新增课程表成功").setCode(200);
    }

}

